//
//  QRCodeViewController.m
//  SinaWebo
//
//  Created by wwj on 16/5/6.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "MyQRCodeViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "QRCodeView.h"
#import "UIImage+Extension.h"
#import <AVFoundation/AVFoundation.h>

@interface MyQRCodeViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) UIImage *navBackImage;
@property (weak, nonatomic) QRCodeView *qrCoderView;
@end

@implementation MyQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavigation];
    
    [self setupQRCodeView];
}


#pragma mark 设置导航栏
-(void)setNavigation{
    // 左边按钮
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTitle:@"关闭" target:self action:@selector(onClickClose)];
    // 右边按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"相册" target:self action:@selector(onClickPhoto)];
    self.navigationItem.title = @"扫一扫";
    _navBackImage = self.navigationController.navigationBar.backIndicatorImage;
    
}

#pragma mark 设置二维码
- (void)setupQRCodeView{
    QRCodeView *qrCoderView = [QRCodeView qrCodeView];
    qrCoderView.frame = self.view.bounds;
    [self.view addSubview:qrCoderView];
    self.qrCoderView = qrCoderView;
    [self.qrCoderView startScanWithBlock:^(NSString *typeStr) {
       // [MBProgressHUD showSuccess:typeStr];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:dict];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.1 alpha:0.7]] forBarMetrics:UIBarMetricsDefault];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:self.navBackImage forBarMetrics:UIBarMetricsDefault];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [super viewWillDisappear:animated];
    
}


- (void)onClickClose{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onClickPhoto{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.delegate = self;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }
}

#pragma mark Photo 代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *photoImg = info[UIImagePickerControllerOriginalImage];
    [self.qrCoderView scanImageWithPhotoImage:photoImg];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
